package com.emm.yixun.marketing.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import com.emm.yixun.basic.service.*;
import com.emm.yixun.common.model.NeedAdvanceRefund;
import com.emm.yixun.common.model.dto.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.emm.yixun.common.model.MortgageRecord;
import com.emm.yixun.common.model.NeedReceiveRefund;
import com.emm.yixun.common.model.enums.BusinessFlagEnum;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.model.enums.HousingFundTypeEnum;
import com.emm.yixun.common.model.enums.ImgTypeEnum;
import com.emm.yixun.common.model.enums.NeedStatusEnum;
import com.emm.yixun.common.model.enums.PaymentMethodEnum;
import com.emm.yixun.common.model.enums.StatementTypeEnum;
@Service("proceedsTransactionalService")
public class ProceedsTransactionalService {
	@Autowired
	private NeedReceiveRefundService needReceiveRefundService;

	@Autowired
	private NeedAdvanceRefundService needAdvanceRefundService;
	
	@Autowired
	private HaveReceiveRefundService haveReceiveRefundService;

	@Autowired
	private HaveAdvanceRefundService haveAdvanceRefundService;
	
	@Autowired
	private MortgageRecordService mortgageRecordService;
	
	@Autowired
	private ImageService imageService;
	

	@Transactional(propagation=Propagation.REQUIRED)
	public void haveReceiveOperationWithTransactional(ProceedsDto proceedsDto, NeedReceiveRefund needReceiveRefund, boolean enoughFlag,HaveReceiveRefundDto hrrdDto) {
		HaveReceiveRefundDto haveReceiveRefundDto = haveReceiveRefundService.create(hrrdDto);
		
		if(StatementTypeEnum.POS_TRANSFER.getNo().equals(proceedsDto.getStatementType())){
			if(StringUtils.isNotEmpty(proceedsDto.getServiceFee()) ){
				//手续费
				HaveReceiveRefundDto haveReceiveRefundFeeDto = new HaveReceiveRefundDto();
				BeanUtils.copyProperties(haveReceiveRefundDto, haveReceiveRefundFeeDto);
				haveReceiveRefundFeeDto.setRealAmount(new BigDecimal(proceedsDto.getServiceFee()));
				haveReceiveRefundFeeDto.setStatementType(Long.parseLong(StatementTypeEnum.POS_TRANSFER.getNo()));
				//手续费皆为一次性支付
				haveReceiveRefundFeeDto.setPaymentMethod(PaymentMethodEnum.LUMP_SUM.getNo());
				//手续费无经纪人、机构
				haveReceiveRefundFeeDto.setAgentName(null);
				haveReceiveRefundFeeDto.setOrgName(null);
				//手续费营收日期定为当天
				haveReceiveRefundFeeDto.setReceivablesDate(Calendar.getInstance().getTime());
				//房款类型: POS手续费
				haveReceiveRefundFeeDto.setHousingFundType(HousingFundTypeEnum.FUNDTYPE_8.getNo());
				//业务标志: POS手续费
				haveReceiveRefundFeeDto.setBusinessFlag(BusinessFlagEnum.BUSINESSFLAG_11.getNo());
				
				haveReceiveRefundService.create(haveReceiveRefundFeeDto);
			}
		}
		
		//变更应收款记录
		//收款金额与剩余收款总额一致,变更待收款记录为已结清
		if(enoughFlag){
			NeedReceiveRefundDto needReceiveRefundDto = new NeedReceiveRefundDto();
			needReceiveRefundDto.setId(needReceiveRefund.getId());
			needReceiveRefundDto.setStatus(NeedStatusEnum.YES.getNo());
			needReceiveRefundService.update(needReceiveRefundDto);
			/*
			 * 修改按揭记录收款状态
			 */
			MortgageRecordDto entity = new MortgageRecordDto();
			entity.setNeedId(needReceiveRefund.getId());
			List<MortgageRecord> mortgageRecords = mortgageRecordService.selectListByDto(entity);
			for (MortgageRecord mortgageRecord : mortgageRecords) {
				mortgageRecord.setMortgageReceivedStatus("2");//已收款
				MortgageRecordDto dto = new MortgageRecordDto();
				BeanUtils.copyProperties(mortgageRecord, dto);
				mortgageRecordService.update(dto);
			}
		}else{
			/*
			 * 修改按揭记录收款状态
			 */
			MortgageRecordDto entity = new MortgageRecordDto();
			entity.setNeedId(needReceiveRefund.getId());
			List<MortgageRecord> mortgageRecords = mortgageRecordService.selectListByDto(entity);
			for (MortgageRecord mortgageRecord : mortgageRecords) {
				mortgageRecord.setMortgageReceivedStatus("1");//部分收款
				MortgageRecordDto dto = new MortgageRecordDto();
				BeanUtils.copyProperties(mortgageRecord, dto);
				mortgageRecordService.update(dto);
			}
		}
		//存储收款凭证
		long haveReceiveRefundId = haveReceiveRefundDto.getId();
		String imgType = "";
		String remark = "";
		String fileArr = proceedsDto.getFileArr();
		if(BusinessTypeEnum.DEPOSIT.getNo().equals(proceedsDto.getBusinessType())){
			imgType = ImgTypeEnum.REC_DEPOSIT.getNo();
			remark = "认筹凭证";
		}else if(BusinessTypeEnum.SUBSCRIBE.getNo().equals(proceedsDto.getBusinessType())){
			imgType = ImgTypeEnum.REC_SUBSCRIBE.getNo();
			remark = "认购凭证";
		}else if(BusinessTypeEnum.SIGNED.getNo().equals(proceedsDto.getBusinessType())){
			imgType = ImgTypeEnum.REC_SIGNED.getNo();
			remark = "签约凭证";
		}else if(BusinessTypeEnum.AREA.getNo().equals(proceedsDto.getBusinessType())){
			imgType = ImgTypeEnum.REC_AREA.getNo();
			remark = "面积补差凭证";
		}
		if(StringUtils.isNotEmpty(fileArr)){
			String fileArrList[] = fileArr.split("[|]");
			for(String file: fileArrList){
				if(StringUtils.isNotEmpty(file)){
					ImageDto imageDto = new ImageDto();
					imageDto.setObjectId(haveReceiveRefundId);
					imageDto.setTitle("收款凭证");
					imageDto.setContent("收款凭证");
					imageDto.setSmallUrl(file);
					imageDto.setImgType(imgType);
					imageDto.setMerchantId(haveReceiveRefundDto.getMerchantId());
					imageDto.setUserCode(haveReceiveRefundDto.getUserCode());
					imageDto.setAccessCode(haveReceiveRefundDto.getAccessCode());
					imageDto.setRemark(remark);
					imageDto.setCreateUserId(haveReceiveRefundDto.getCreateUserId());
					imageDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
					imageService.create(imageDto);
				}
			}
		}
		
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void haveAdvanceOperationWithTransactional(ProceedsDto proceedsDto, NeedAdvanceRefund needReceiveRefund, HaveAdvanceRefundDto hrrdDto, boolean enoughFlag) {
		HaveAdvanceRefundDto haveAdvanceRefundDto = haveAdvanceRefundService.create(hrrdDto);

		//变更应收款记录
		//收款金额与剩余收款总额一致,变更待收款记录为已结清
		if(enoughFlag){
			NeedAdvanceRefundDto needAdvanceRefundDto = new NeedAdvanceRefundDto();
            needAdvanceRefundDto.setId(needReceiveRefund.getId());
            needAdvanceRefundDto.setStatus(NeedStatusEnum.YES.getNo());
			needAdvanceRefundService.update(needAdvanceRefundDto);
			/*
			 * 修改按揭记录收款状态
			 */
			MortgageRecordDto entity = new MortgageRecordDto();
			entity.setNeedId(needReceiveRefund.getId());
			List<MortgageRecord> mortgageRecords = mortgageRecordService.selectListByDto(entity);
			for (MortgageRecord mortgageRecord : mortgageRecords) {
				mortgageRecord.setMortgageReceivedStatus("2");//已收款
				MortgageRecordDto dto = new MortgageRecordDto();
				BeanUtils.copyProperties(mortgageRecord, dto);
				mortgageRecordService.update(dto);
			}
		}else{
			/*
			 * 修改按揭记录收款状态
			 */
			MortgageRecordDto entity = new MortgageRecordDto();
			entity.setNeedId(needReceiveRefund.getId());
			List<MortgageRecord> mortgageRecords = mortgageRecordService.selectListByDto(entity);
			for (MortgageRecord mortgageRecord : mortgageRecords) {
				mortgageRecord.setMortgageReceivedStatus("1");//部分收款
				MortgageRecordDto dto = new MortgageRecordDto();
				BeanUtils.copyProperties(mortgageRecord, dto);
				mortgageRecordService.update(dto);
			}
		}

		//存储收款凭证
		long haveReceiveRefundId = haveAdvanceRefundDto.getId();
		String imgType = ImgTypeEnum.REC_SIGNED.getNo();
		String remark = "签约凭证";
		String fileArr = proceedsDto.getFileArr();

		if(StringUtils.isNotEmpty(fileArr)){
			String fileArrList[] = fileArr.split("[|]");
			for(String file: fileArrList){
				if(StringUtils.isNotEmpty(file)){
					ImageDto imageDto = new ImageDto();
					imageDto.setObjectId(haveReceiveRefundId);
					imageDto.setTitle("收款凭证");
					imageDto.setContent("收款凭证");
					imageDto.setSmallUrl(file);
					imageDto.setImgType(imgType);
					imageDto.setMerchantId(haveAdvanceRefundDto.getMerchantId());
					imageDto.setUserCode(haveAdvanceRefundDto.getUserCode());
					imageDto.setAccessCode(haveAdvanceRefundDto.getAccessCode());
					imageDto.setRemark(remark);
					imageDto.setCreateUserId(haveAdvanceRefundDto.getCreateUserId());
					imageDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
					imageService.create(imageDto);
				}
			}
		}
	}
}
